package cn.iocoder.yudao.module.system.service.productionplanissued;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.system.controller.admin.productionplan.vo.ProductionPlanRespVO;
import cn.iocoder.yudao.module.system.controller.admin.productionplandetail.vo.ProductionPlanDetailRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.productionplan.ProductionPlanDO;
import cn.iocoder.yudao.module.system.dal.dataobject.productionplandetail.ProductionPlanDetailDO;
import cn.iocoder.yudao.module.system.dal.mysql.productionplandetail.ProductionPlanDetailMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.productionplanissued.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.productionplanissued.ProductionPlanIssuedDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.productionplanissued.ProductionPlanIssuedMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 生成计划下发批次 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ProductionPlanIssuedServiceImpl implements ProductionPlanIssuedService {

    @Resource
    private ProductionPlanIssuedMapper productionPlanIssuedMapper;
    @Resource
    private ProductionPlanDetailMapper productionPlanDetailMapper;

    @Override
    public Integer createProductionPlanIssued(ProductionPlanIssuedSaveReqVO createReqVO) {
        QueryWrapper<ProductionPlanIssuedDO> planIssuedDOQueryWrapper = new QueryWrapper<>();
        planIssuedDOQueryWrapper.gt("create_time", new Date());
        Long num = productionPlanIssuedMapper.selectCount(planIssuedDOQueryWrapper);
        // 获取当前日期
        Date date = new Date();

        // 定义日期格式
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        // 格式化日期为字符串
        String formattedDate = formatter.format(date);
        // 插入
        ProductionPlanIssuedDO productionPlanIssued = BeanUtils.toBean(createReqVO, ProductionPlanIssuedDO.class);
        productionPlanIssued.setBatchNumber(formattedDate+num.toString());
        productionPlanIssuedMapper.insert(productionPlanIssued);
        List<Integer> ids = new ArrayList<>();
        for (ProductionPlanDetailRespVO item:createReqVO.getDetailRespVOS()) {
            ids.add(item.getId());
        }
        QueryWrapper<ProductionPlanDetailDO> detailDOQueryWrapper = new QueryWrapper<>();
        detailDOQueryWrapper.in("id",ids);
        detailDOQueryWrapper.eq("plan_id",createReqVO.getPlanId());
        detailDOQueryWrapper.eq("issued",1);
        ProductionPlanDetailDO planDetailDO = new ProductionPlanDetailDO();
        planDetailDO.setIssued(2);
        planDetailDO.setIssuedBatchNumber(productionPlanIssued.getBatchNumber());
        planDetailDO.setIssuedTime(LocalDateTime.now());
        productionPlanDetailMapper.update(planDetailDO,detailDOQueryWrapper);
        // 返回
        return productionPlanIssued.getId();
    }

    @Override
    public void updateProductionPlanIssued(ProductionPlanIssuedSaveReqVO updateReqVO) {
        // 校验存在
        validateProductionPlanIssuedExists(updateReqVO.getId());
        // 更新
        ProductionPlanIssuedDO updateObj = BeanUtils.toBean(updateReqVO, ProductionPlanIssuedDO.class);
        productionPlanIssuedMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductionPlanIssued(Integer id) {
        // 校验存在
        validateProductionPlanIssuedExists(id);
        // 删除
        productionPlanIssuedMapper.deleteById(id);
    }

    @Override
        public void deleteProductionPlanIssuedListByIds(List<Integer> ids) {
        // 校验存在
        validateProductionPlanIssuedExists(ids);
        // 删除
        productionPlanIssuedMapper.deleteByIds(ids);
        }

    private void validateProductionPlanIssuedExists(List<Integer> ids) {
        List<ProductionPlanIssuedDO> list = productionPlanIssuedMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PRODUCTION_PLAN_ISSUED_NOT_EXISTS);
        }
    }

    private void validateProductionPlanIssuedExists(Integer id) {
        if (productionPlanIssuedMapper.selectById(id) == null) {
            throw exception(PRODUCTION_PLAN_ISSUED_NOT_EXISTS);
        }
    }

    @Override
    public ProductionPlanIssuedDO getProductionPlanIssued(Integer id) {
        return productionPlanIssuedMapper.selectById(id);
    }

    @Override
    public PageResult<ProductionPlanIssuedRespVO> getProductionPlanIssuedPage(ProductionPlanIssuedPageReqVO pageReqVO) {

        PageResult<ProductionPlanIssuedDO> planDOPageResult = productionPlanIssuedMapper.selectPage(pageReqVO);
        PageResult<ProductionPlanIssuedRespVO> data = BeanUtils.toBean(planDOPageResult, ProductionPlanIssuedRespVO.class);
        List<String> planIds = new ArrayList<>();
        for (ProductionPlanIssuedRespVO item:data.getList()) {
            planIds.add(item.getBatchNumber());
        }

        QueryWrapper<ProductionPlanDetailDO> detailDOQueryWrapper = new QueryWrapper<>();
        detailDOQueryWrapper.in("issued_batch_number",planIds);
        List<ProductionPlanDetailDO> detailDOS = productionPlanDetailMapper.selectList(detailDOQueryWrapper);

        for (ProductionPlanIssuedRespVO item:data.getList()) {
            List<ProductionPlanDetailRespVO> detailRespVOS = new ArrayList<>();
            for(ProductionPlanDetailDO detail:detailDOS){
                if(item.getBatchNumber().equals(detail.getIssuedBatchNumber())){
                    ProductionPlanDetailRespVO detailRespVO = BeanUtils.toBean(detail, ProductionPlanDetailRespVO.class);
                    detailRespVOS.add(detailRespVO);
                }
            }
            item.setDetailRespVOS(detailRespVOS);
        }

        return data;
    }

}
